package viettravel.guiandevent.map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import android.annotation.SuppressLint;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class GetFriendLocationMapV2 extends SherlockFragmentActivity {
	private Double mLat;
	private Double mLng;
	private GoogleMap map;
	private String mFriendList;
	private boolean flag = true;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(Constants.THEME);
		setContentView(R.layout.gmap_api_v2);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);
		actionBar.setTitle(getString(R.string.newplace_getlatlng_title));
		SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.googleMapAPIMap);

		map = fm.getMap();
		map.setMyLocationEnabled(true);
		map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
			@Override
			public void onMyLocationChange(Location location) {
				// TODO Auto-generated method stub
				mLat = location.getLatitude();
				mLng = location.getLongitude();
				if (flag && mLat != null && mLng != null) {
					map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(
							mLat, mLng)));
					map.animateCamera(CameraUpdateFactory.zoomTo(14), 3000,
							null);
					flag = false;
				}
			}
		});

		map.setOnMapLongClickListener(new OnMapLongClickListener() {
			@Override
			public void onMapLongClick(LatLng latlng) {
				map.clear();
				GooglemapCommon.addMarker(map, latlng,
						R.drawable.ic_my_position);
				mLat = latlng.latitude;
				mLng = latlng.longitude;
			}
		});
		try {
			Bundle extra = getIntent().getExtras();
			mFriendList = extra
					.getString(Constants.EXTRA_GETFRIEND);
			JSONArray friendList = new JSONArray(mFriendList);
			for(int i = 0; i < friendList.length(); i++){
				JSONObject friend = friendList.getJSONObject(i);
				JSONObject friendLocation = friend.getJSONObject(Constants.TAG_FB_CURRENT_LOCATION);
				Double lat = friendLocation.getDouble(Constants.TAG_FB_LATITUE);
				Double lng = friendLocation.getDouble(Constants.TAG_FB_LONGTITUTE);
				Log.v(Constants.TAG + "LatLng",lat + ", "+ lng);
				GooglemapCommon.addMarker(map, new LatLng(lat, lng), R.drawable.ic_a_position);
			}
			Log.v(Constants.TAG, friendList.length()+"");
		} catch (Exception e) {
			Log.v(Constants.TAG, "loi");
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		/*case R.id.newplace_getlatlng_done: {
			Log.v(Constants.TAG, "Come on");
			Log.v(Constants.TAG + "getlatlng", mPlaceName);
			Log.v(Constants.TAG + "getlatlng", mLat + "");
			Log.v(Constants.TAG + "getlatlng", mLng + "");
			Intent i = new Intent();
			i.putExtra(Constants.EXTRA_PLACE_NAME, mPlaceName);
			i.putExtra(Constants.EXTRA_PLACE_LONG, mLat);
			i.putExtra(Constants.EXTRA_PLACE_LAT, mLng);
			setResult(RESULT_OK, i);
			finish();
		}*/
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu _menu) {
		_menu.add(Menu.NONE, R.id.newplace_getlatlng_done, Menu.NONE,
				this.getString(R.string.newplace_getlatlng_done))
				.setIcon(R.drawable.abs__ic_cab_done_holo_light)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
		return super.onCreateOptionsMenu(_menu);
	}
}
